package com.nbot.server.config;

import com.nbot.server.BotServer;
import com.nbot.server.properties.NbotProperties;
import com.nbot.server.workers.DbWorker;
import com.nbot.server.workers.OpcWorker;
import com.nbot.server.workers.OracleWorker;
import com.nbot.server.workers.impl.DbWorkerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * NbotServer的配置类
 */
@Configuration
public class NbotServerConfig {

    @Autowired
    private NbotProperties nbotProperties;

    /**
     * 本地数据处理类
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "dbWorker")
    public DbWorker dbWorker() {
        return new DbWorkerImpl();
    }

    /**
     * opc数据处理类
     */
    @Bean
    @ConditionalOnMissingBean(name = "opcWorker")
    public OpcWorker opcWorker() {
        Class clazz = null;
        OpcWorker opcWorker = null;
        try {
            clazz = Class.forName(nbotProperties.getOpcWorkClass());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (clazz != null) {
            try {
                opcWorker = (OpcWorker) clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return opcWorker;

    }

    /**
     * BotServer
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "botServer")
    public BotServer botServer() {
        return new BotServer();
    }

    @Bean
    @ConditionalOnMissingBean(name = "oracleWorker")
    public OracleWorker oracleWorker() {
        Class clazz = null;
        OracleWorker oracleWorker = null;
        try {
            clazz = Class.forName(nbotProperties.getOracleWorkerClass());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (clazz != null) {
            try {
                oracleWorker = (OracleWorker) clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return oracleWorker;
    }

}
